package Ms

import (
	"db2s/global"
)

type TableSum struct{}

/*
Rows oracle 查询表的统计信息中行数
*/
func (ms TableSum) Rows(s global.TableSumInput) (rowsCount global.RowsReturnResult, err error) {
	//var (
	//	event = "[sqlserverRows]"
	//	f1    any
	//)
	//rowsCount.BaseResult.Sql = fmt.Sprintf("SELECT b.rows as tableRows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE b.indid in (0,1) and DB_NAME()='%v' and a.name = '%v' ORDER BY a.name,b.rows DESC", s.TableInfo.Schema, s.TableInfo.BackendTableName)
	////收集统计信息
	//if rowsCount.BaseResult.TimeOut, f1, err = execSql(s, rowsCount.BaseResult.Sql, dataDispos.Map); err != nil || f1 == nil {
	//	if err != nil {
	//		err = errors.New(fmt.Sprintf("%v %v", event, err))
	//	}
	//	return
	//}
	//if rowsCount.Result, err = strconv.ParseUint(fmt.Sprintf("%v", f1.(map[string]any)["tableRows"]), 10, 64); err != nil {
	//	err = errors.New(fmt.Sprintf("%v table sum conversion int failed. process:{sql:%v,shard:%v,db:%v,result:%v,err:%v}", event, rowsCount.BaseResult.Sql, "single", s.Db, f1, err))
	//	return
	//}
	//if s.TableInfo.RowsLimit > 0 {
	//	rowsCount.Result = uint64(s.TableInfo.RowsLimit)
	//}
	return
}

func (ms TableSum) Count(s global.TableSumInput) (rowsCount global.CountReturnResult, err error) {
	//var (
	//	event = "[sqlserverCount]"
	//	f1    any
	//)
	////收集统计信息
	//rowsCount.BaseResult.Sql = fmt.Sprintf("select count(*) as tableRows \n"+
	//	"from %v where 1=1 \n"+
	//	"%v %v %v OPTION (MAXDOP 4)",
	//	tableObject(s.TableInfo.Schema, s.TableInfo.BackendTableName, s.TableInfo.PartitionName),
	//	whereFilter(s.SqlFilterFunc.WhereSql), whereFilter(s.SqlFilterFunc.WhereAdd), rowsLimitC(s.TableInfo.RowsLimit))
	//if rowsCount.BaseResult.TimeOut, f1, err = execSql(s, rowsCount.BaseResult.Sql, dataDispos.Map); err != nil || f1 == nil {
	//	if err != nil {
	//		err = errors.New(fmt.Sprintf("%v %v", event, err))
	//	}
	//	return
	//}
	//if rowsCount.Result, err = strconv.ParseUint(fmt.Sprintf("%v", f1.(map[string]any)["tableRows"]), 10, 64); err != nil {
	//	err = errors.New(fmt.Sprintf("%v table sum conversion int failed. process:{sql:%v,shard:%v,db:%v,result:%v,err:%v}", event, rowsCount.BaseResult.Sql, "single", s.Db, f1, err))
	//	return
	//}

	return
}
func (ms TableSum) CountDirect(s global.TableSumInput) (bool, int64) {
	switch {
	case len(s.IndexColumn) == 0:
		return true, 1
	default:
		return false, 1
	}
}
